<html>
  <head>
    <meta charset="utf-8"/>
	<!--<meta http-equiv="refresh" content="30">-->
    <style>
      table{
        cellpadding:0px;
        cellspacing:0px;
		border-collapse:collapse;
      }
	  td{
	    margin:0px 0px 0px 0px;
	  }
      p{
        padding:0px;
        margin:0px;
      }
      div{
        background-color:#669900;
        width:10px;
		margin-right: 0px; margin-bottom: 0px;
      }

	  div.yesterday{
        background-color:#996600;
        width:10px;
		margin-right: 0px; margin-bottom: 0px;
	  }

      #div1{
        height:200px;
      }

    </style>
	<title>商业化-监控</title>
	<h1>商业化-服务器实时状态监控（分钟）</h1>
  </head>

  <body>

  <button style="width:200px; height:35px; background-color:#afffff;" onclick="refresh_page('business')" >商业服(business)</button>
<button style="width:200px; height:35px; background-color:#afffff;" onclick="refresh_page('battlepass')" >商业通行证服(battlepass)</button>
<button style="width:200px; height:35px; background-color:#afffff;" onclick="refresh_page('business_welfare')" >商业福利服(business_welfare)</button>
<button style="width:200px; height:35px; background-color:#afffff;" onclick="refresh_page('business_advert')" >商业广告服(business_advert)</button>
<button style="width:200px; height:35px; background-color:#afffff;" onclick="refresh_page('business_mall')" >商业商城服(business_mall)</button>
<?php
	require "monitor.php";

	$server_type = $_GET['server_type'];

	// 排序
	function sort_func( $a, $b ){
		$a_array_info = explode( "\n",  trim($a));
		$b_array_info = explode( "\n",  trim($b));

		$a_name = "";
		$b_name = "";
		$a_net = "";
		$b_net = "";

		for ( $i=0; $i<count($a_array_info); $i++){
			$a_array_value = explode( "==",  trim($a_array_info[$i]));
			switch ( $a_array_value[0] ) {
				case "name":	$a_name = $a_array_value[1];	break;
				case "net":		$a_net = $a_array_value[1];		break;
			}
		}
		for ( $i=0; $i<count($b_array_info); $i++){
			$b_array_value = explode( "==",  trim($b_array_info[$i]));
			switch ( $b_array_value[0] ) {
				case "name":	$b_name = $b_array_value[1];	break;
				case "net":		$b_net = $b_array_value[1];		break;
			}
		}
		
		if ($a_name < $b_name) return -1;
		if ($a_name > $b_name) return 1;

		if ((int)$a_net > (int)$b_net) return -1;
		if ((int)$a_net < (int)$b_net) return 1;
		
		return 1;
	}
	
	// 调用内部网页获得信息
	global $monitor_info_url;
	$full_url = $monitor_info_url . "&server_type=" . $server_type;
	$ret_server_ = file_get_contents($full_url);

	// 按行分割
	$array_line = explode("||", $ret_server_);
	$server_count = count($array_line); 
	usort($array_line, 'sort_func');

	echo '<h3>服务器状态[' . $server_count . ']</h3>';
	//echo '<table border=1 cellpadding=10 > <tr><th>服务器</th><th>CPU</th><th>内存</th><th>硬盘</th><th>名称</th><th>时间</th></tr>';
	echo '<table border=1 cellpadding=10 > <tr><th>服务器</th><th>CPU</th><th>内存</th><th>硬盘</th><th>网络</th><th>关键进程数</th><th>名称</th><th>时间</th></tr>';

	for( $i=0; $i<$server_count; $i++ ){
		// 按行分割
		$array_info = explode( "\n",  trim($array_line[$i]) );
		echo '<tr>';
		for ( $j=0; $j< count($array_info); $j++ ){
			// 按==分割
			$array_hardware_info = explode( "==",  trim($array_info[$j]) );
			switch ( $array_hardware_info[0] ) {
				case "name":
					echo '<td>' . $array_hardware_info[1] . '</td>' ;
					break;
				case "fn":
					echo '<td>' . $array_hardware_info[1] . '</td>' ;
					break;
				case "cpu":
						$cpu_info = explode( ",",  trim($array_hardware_info[1]) );
					$cpu_load1 = $cpu_info[0];
					$cpu_load2 = $cpu_info[1];
					$cpu_load3 = $cpu_info[2];
					$cpu_cores = $cpu_info[3];
					$cpu_percent = (int)(($cpu_load1/$cpu_cores) * 100);
					if ($cpu_percent >= 120)
					{
						echo '<td style="color:orange">' . (int)($cpu_load1*100) . '%~' . (int)($cpu_load2*100) . '%' . '(' .  $cpu_cores .  ')' . '</td>';
					}elseif ($cpu_percent >= 100)
					{
						echo '<td style="color:blue">' . (int)($cpu_load1*100) . '%~' . (int)($cpu_load2*100) . '%' . '(' .  $cpu_cores .  ')' . '</td>';
					}else{
						echo '<td>' . (int)($cpu_load1*100) . '%~' . (int)($cpu_load2*100) . '%' . '(' .  $cpu_cores .  ')' . '</td>';
					}
					break;
				case "mem_info":
					$mem_info = explode( ",",  trim($array_hardware_info[1]) );
					$used_mem = (int)$mem_info[0];
					$total_mem = (int)$mem_info[1];
					$mem_percent = (int)( $used_mem / (int)($total_mem) * 100);
					if ($mem_percent < 70)
					{
						echo '<td>' . $mem_percent . '% ('. $used_mem . 'm/' . $total_mem . 'm)</td>';
					}else{
						echo '<td style="color:blue">' . $mem_percent . '% ('. $used_mem . 'm/' . $total_mem . 'm)</td>';
					}
					break;
				case "df":
					$df_info = explode( " ",  trim($array_hardware_info[1]) );
					echo '<td>';
					for ( $d=1; $d<count($df_info); $d++ ){
						if ( $d>2 && $df_info[$d-1] == $df_info[$d] ){
							// exclude the same item
						}else{
							echo ' ' . $df_info[$d];
						}
					}
					//echo '</td>';
					break;
				case "net":
					$net_rate = number_format($array_hardware_info[1]/(1024*1024), 2);
					if ($net_rate < 180)
					{
						echo '<td>' . $net_rate . 'm</td>';
					}else{
						echo '<td style="color:blue">' . $net_rate . 'm</td>';
					}
					break;
				case "pids":
					echo '<td>' . $array_hardware_info[1] . '</td>' ;
					break;
				case "time":
					echo '<td>' . $array_hardware_info[1] . '</td>';
					break;
				default:
					break;
			}
		}
		echo '</tr>';
	}

	echo '</table>';
?>

	<br>
</div>


<script type="text/javascript">
	function refresh_page(server_type){
		window.location.href="/oa/miniw_business.php?server_type=" + server_type;
	}
	function auto_refresh_page(){
		window.location.reload();
	}
	setTimeout('auto_refresh_page()',30*1000); //指定30秒刷新一次
</script>
</body>
</html>
